﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLServerDAL
{
    public class Content : BaseDAL, IContent
    {
        public int Insert(Models.Content model)
        {
            if (model.ID <= 0)
                model.ID = Db.CreateKey("Content");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@KeyName", SqlDbType.VarChar,50),
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
                    new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", DbType.DateTime),
					new SqlParameter("@Text", SqlDbType.VarChar)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.KeyName;
            parameters[2].Value = model.ID;
            parameters[3].Value = model.ClassID;
            parameters[4].Value = model.UserID;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Text;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Content([Title],[KeyName],[ID],[ClassID],[UserID],[CreateTime],[Text]) values (@Title,@KeyName,@ID,@ClassID,@UserID,@CreateTime,@Text)", parameters);
        }

        public int Update(Models.Content model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@KeyName", SqlDbType.VarChar,50),
					new SqlParameter("@Text", SqlDbType.VarChar),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.KeyName;
            parameters[2].Value = model.Text;
            parameters[3].Value = model.ID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "update JNCms_Content set [Title]=@Title,[KeyName]=@KeyName,[Text]=@Text where [ID]=@ID", parameters);
        }

        public int Delete(int id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = id;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "delete from JNCms_Content where [ID]=@ID", parameters);
        }

        public List<Models.Content> GetList(int id,int pageIndex, int pageSize)
        {
            List<Models.Content> list = new List<Models.Content>();
            if (pageSize > 0)
            {
                string sql;
                if (pageIndex == 1)
                    sql = pageSize.ToString();
                else
                    sql = string.Concat(Convert.ToString(pageSize * (pageIndex - 1)), ",", Convert.ToString(pageSize * pageIndex));

                SqlParameter parameters = new SqlParameter("@ClassID", SqlDbType.Int, 4);
                parameters.Value = id;

                using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, string.Concat("select [Title],[KeyName],[ID],[ClassID],[UserID],[CreateTime],'' as [Text] from [JNCms_Content] where [ClassID]=@ClassID limit ", sql), parameters))
                {
                    while (rd.Read())
                    {
                        list.Add(Db.Entity.PopulateEntity<Models.Content>(rd));
                    }
                    //list = Db.Entity.PopulateEntityCollection<Models.Content>(rd);
                }

            }
            return list;
        }

        public Models.Content GetItem(string item)
        {
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from [JNCms_Content] where [KeyName]=@KeyName limit 1", new SqlParameter("@KeyName", item)))
            {
                if (rd.Read())
                {
                    return Db.Entity.PopulateEntity<Models.Content>(rd);
                }
            }
            return null;
        }
    }
}
